//
//  HWHomeRecentGameCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/21.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeRecentGameCell: UITableViewCell {
    var collectionView:UICollectionView!
    let collectionProtocol = HWHomeRecentGameCollectionProtocol()
    var models:[HWHomeGameModel]?{
        didSet {
            self.collectionProtocol.models = models
            self.collectionView.reloadData()
        }
    }
    var didClickRecentGame:((_:HWHomeGameModel)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWHomeRecentGameCell) {
        let reUse:String = "HWHomeRecentGameCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeRecentGameCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeRecentGameCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = .white
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWHomeRecentGameCollectionCell.self, forCellWithReuseIdentifier: "HWHomeRecentGameCollectionCell")
        contentView.addSubview(collectionView)
    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(10*widthScale).priority(.low)
            make.bottom.equalTo(0*widthScale)
            make.left.right.equalToSuperview()
            make.height.equalTo(130*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWHomeRecentGameCell {
    func addAction() -> Void {
        self.collectionProtocol.didClickRecentGame = {
            mod in
            self.didClickRecentGame?(mod)
        }
    }
}




class HWHomeRecentGameCollectionProtocol: UICollectionViewCell {
    var models:[HWHomeGameModel]?
    var didClickRecentGame:((_:HWHomeGameModel)->())?
}

extension HWHomeRecentGameCollectionProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.didClickRecentGame?(models?[indexPath.row] ?? HWHomeGameModel())
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 5*widthScale, bottom: 0, right: 5*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: screenWidth/5, height: 128*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  models?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let gameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeRecentGameCollectionCell", for: indexPath) as! HWHomeRecentGameCollectionCell
        
        gameCell.model = models?[indexPath.row]
        return gameCell
        
        
    }
    
}
import Kingfisher
class HWHomeRecentGameCollectionCell: UICollectionViewCell {
    var logoImageView:SDAnimatedImageView!
    var nameLabel:UILabel!
    var timeLabel:UILabel!
    var detailLabel:UILabel!
    
    var model:HWHomeGameModel? {
        didSet {
            logoImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name.limitedLengthLabelText(limit: 4)
            timeLabel.text = model?.type
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWHomeRecentGameCollectionCell {
    private func initUI() -> Void {
        contentView.backgroundColor = .clear
        
        logoImageView = SDAnimatedImageView()
        logoImageView.layer.cornerRadius = 15*widthScale
        logoImageView.layer.masksToBounds = true
        contentView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 11*widthScale)
        nameLabel.textAlignment = .center
        nameLabel.numberOfLines = 1
        contentView.addSubview(nameLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#979797", font: 11*widthScale)
        timeLabel.textAlignment = .center
        timeLabel.numberOfLines = 1
        contentView.addSubview(timeLabel)
        
        detailLabel = HWLabel.createLabel(title: "详情", color: "#FA8C08", font: 13*widthScale)
        detailLabel.textAlignment = .center
        detailLabel.numberOfLines = 1
        detailLabel.layer.cornerRadius = 15*widthScale
        detailLabel.layer.borderWidth = 1
        detailLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FA8C08").cgColor
        contentView.addSubview(detailLabel)
        
    }
    
    private func initFrame() -> Void {
        
        logoImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.width.height.equalTo(54*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(logoImageView.snp.bottom).offset(7*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(4*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(timeLabel.snp.bottom).offset(5*widthScale)
            make.centerX.equalToSuperview()
            make.width.equalTo(49*widthScale)
            make.height.equalTo(30*widthScale)
        }
    }
}

